@include("user.head")
	<!-- 公共头部 -->

	<div id="content_person">
	<div class="row">
		<div class="col-xs-3">
		@include('user.uc.navigation')
		</div>
		<div class="col-xs-9">
			<div class="panel panel-default">
				<div class="panel-heading">
					<h3 class="panel-title">编辑行业信息</h3>
				</div>
				<div class="panel-body">
					<div class="form-horizontal" id="Form" method="post" action="edit_industryinfo/update">
						<fieldset>
							<div class="form-group">
								<label for="inputEmail" class="col-xs-3 control-label no_padding_right">员工人数:</label>
								<div class="col-xs-6">
									<input class="form-control input_size" name="staff_num" id="inputEmail" placeholder="" type="text" />
								</div>
							</div>

							<div class="form-group">
								<label for="inputEmail" class="col-xs-3 control-label no_padding_right">工程技术人员人数:</label>
								<div class="col-xs-6">
									<input class="form-control input_size" name="tech_num" id="inputEmail" placeholder="" type="text" />
								</div>
							</div>

							<div class="form-group">
								<label for="inputEmail" class="col-xs-3 control-label no_padding_right">高级技术人数:</label>
								<div class="col-xs-6">
									<input class="form-control input_size" name="senior_tech_num" id="inputEmail" placeholder="" type="text" >
								</div>
							</div>

							<div class="form-group domainGroup">
					            <label for="inputEmail" class="col-xs-3 control-label no_padding_right">应用领域:</label>
					            <div class="col-xs-3">
					              <select class="form-control input_size app_domain">
					              	<option value="-1">请选择</option>
					              	@foreach($domains as $value)
					              	<option value="<%$value['id']%>"><%$value['cate_name']%></option>
					              	@endforeach
					              </select>
					            </div>
					            <div class="col-xs-3 hide">
					              <select class="form-control input_size child_domain"></select>
					            </div>
					            <div class="col-xs-3 no_padding_both">
					              <button class="btn btn-primary" id="addDomain">添加</button>
					            </div>
					          </div>

					          <div class="form-group saveTechGroup">
					            <label for="inputEmail" class="col-xs-3 control-label no_padding_right">节能技术:</label>
					            <div class="col-xs-6">
					              <select class="form-control input_size save_tech">
					              	@foreach ($techs as $value)
					              	<option value="<%$value['id']%>"><%$value['cate_name']%></option>
					              	@endforeach
					              </select>
					            </div>
					            <div class="col-xs-3 no_padding_both">
					            	<button class="btn btn-primary" id="addTech">添加</button>
					            </div>
					          </div>

							<div class="form-group">
								<label class="col-xs-3 control-label no_padding_right">有无银行资信信息:</label>
								<div class="col-xs-6">
									<div class="radio">
										<label>
											<input name="has_bank_credit_info" id="" value="0" checked="" type="radio" />
											无
										</label>
										<label>
											<input name="has_bank_credit_info" id="" value="1" type="radio" />
											有
										</label>
									</div>
								</div>
							</div>

							<div class="form-group hide bank_credit">
								<label for="inputEmail" class="col-xs-3 control-label no_padding_right">银行资信等级:</label>
								<div class="col-xs-6">
									<input class="form-control input_size" name="bank_credit_level" id="inputEmail" placeholder="" type="text">
								</div>
							</div>

							<div class="form-group hide bank_credit">
								<label for="inputEmail" class="col-lg-3 control-label no_padding_right">银行资信证书</label>
								<div class="col-lg-6">
									<a href="javascript:;" class="file btn btn-primary">	上传
										<input type="file" name="imgFile" name="" id="bank_copy">
										<input type="hidden" name="bank_credit_copy" id="fileName">
									</a>
									<span id="enwName"></span>
								</div>
							</div>

							<div class="form-group hide bank_credit">
								<label for="inputEmail" class="col-xs-3 control-label no_padding_right">银行贷款卡卡号:</label>
								<div class="col-xs-6">
									<input class="form-control input_size" name="bank_card_code" id="inputEmail" placeholder="" type="text">
								</div>
							</div>

							<div class="form-group">
								<label for="inputEmail" class="col-xs-3 control-label no_padding_right">现有总资产:</label>
								<div class="col-xs-6">
									<input class="form-control input_size" name="total_capital" id="inputEmail" placeholder="" type="text">
								</div>
							</div>

							<div class="form-group">
								<label for="inputEmail" class="col-xs-3 control-label no_padding_right">净资产:</label>
								<div class="col-xs-6">
									<input class="form-control input_size" name="net_capital" id="inputEmail" placeholder="" type="text">
								</div>
							</div>

							<div class="form-group">
								<label for="inputEmail" class="col-xs-3 control-label no_padding_right">产业负债率:</label>
								<div class="col-xs-6">
									<input class="form-control input_size" name="liabilities_rate" autocomplete="of" id="inputEmail" placeholder="" type="text">
								</div>
							</div>

							<div class="form-group">
								<label for="textArea" class="col-xs-3 control-label label_size">需要说明的其他情况</label>
								<div class="col-xs-6">
									<textarea class="form-control text_size" rows="3" name="note" id="textArea"></textarea>
									<span class="help-block"></span>
								</div>
							</div>

							<div class="form-group">
								<div class="col-xs-12 text-center">
									<button type="button" id="sub" class="btn btn-primary ">保存修改</button>
								</div>
							</div>
						</fieldset>
					</div>
				</div>
			</div>
		</div>
	</div>
	</div>

	<!-- 公共底部 -->
	@include("user.footer")
	<!-- 公共底部 -->
	@include("user.common")
</body>
<script type="text/html" id="domainTemp">
<option value="">请选择</option>
{%each list as value}
<option value="{%value.id}">{%value.cate_name}</option>
{%/each}
</script>

<script type="text/html" id="domainSelectTemp">
	<div class="form-group domainGroup">
	  <label for="inputEmail" class="col-xs-3 control-label no_padding_right"></label>
	  <div class="col-xs-3">
	    <select class="form-control input_size app_domain"></select>
	  </div>
	  <div class="col-xs-3 hide">
	    <select class="form-control input_size child_domain"></select>
	  </div>
	  <div class="col-xs-3 no_padding_both">
	  	<button class="btn btn-warning deleteDomain">删除</button>
	  </div>
	</div>
</script>

<script type="text/html" id="saveTechTemp">
	<div class="form-group saveTechGroup">
      <label for="inputEmail" class="col-xs-3 control-label no_padding_right"></label>
      <div class="col-xs-6">
        <select class="form-control input_size save_tech"></select>
      </div>
      <div class="col-xs-3 no_padding_both">
      	<button class="btn btn-warning deleteTech">删除</button>
      </div>
    </div>
</script>

<script type="text/javascript">
var isasync = false;

var hasCreditInfo = false;

$.get('edit_industryinfo/list', function(data){
	if(data.success){
		$('input[name=staff_num]').val(data.data.staff_num);
		$('input[name=tech_num]').val(data.data.tech_num);
		$('input[name=senior_tech_num]').val(data.data.senior_tech_num);
		$('input[name=has_bank_credit_info]').each(function(k, v){
			if($(this).val() == data.data.has_bank_credit_info){
				$(this).attr("checked", "checked");
			}
		})
		$('input[name=bank_credit_level]').val(data.data.bank_credit_level);
		$('input[name=bank_credit_copy]').val(data.data.bank_credit_copy);
		$('input[name=bank_card_code]').val(data.data.bank_card_code);
		$('input[name=total_capital]').val(data.data.total_capital);
		$('input[name=net_capital]').val(data.data.net_capital);
		$('input[name=liabilities_rate]').val(data.data.liabilities_rate);
		$('textarea[name=note]').val(data.data.note);
		$.each(data.data.saver_tech, function(k, v){
			$(".save_tech").eq(k).val(v.cate_id);
			$("#addTech").click();
		})
		$(".deleteTech").last().click();
		if(data.data.has_bank_credit_info == 1)
		{
			hasCreditInfo = true;
			$(".bank_credit").removeClass("hide");
		}
		$.each(data.data.saver_domain, function(k, v){
			$(".app_domain").eq(k).val(v.domain.parent_id);
			$(".app_domain").eq(k).change();
			$(".child_domain").eq(k).val(v.cate_id);
			$("#addDomain").click();
		})
		$(".deleteDomain").last().click();

		isasync = true;
	}else{

	}
})

var appDomainSelect;
$(".app_domain").last().bind("change", function(){
	appDomainSelect = $(this);
	$.ajax({
		url : "<%url('uc/get_domains')%>?parent_id=" + $(this).val(),
		type: "get",
		async : isasync,
		success:function(data){
			if(data.success){
				var html = template("domainTemp", {list: data.data});
				appDomainSelect.parent().next().removeClass("hide");
				appDomainSelect.parent().next().find("select").html(html);
			}
		}

	})

});

$("#addDomain").bind("click", function(){
	var html = template("domainSelectTemp", {});
	$(html).insertAfter($(".domainGroup").last());
	$(".deleteDomain").unbind("click");
	$(".deleteDomain").bind("click", function(){
		console.log($(this));
		$(this).parents(".domainGroup").remove();
	})
	$.ajax({
		url : "<%url('uc/get_domains')%>",
		type : "get",
		async : isasync,
		success:function(data){
			if(data.success){
				var html = template("domainTemp", {list: data.data});
				$(".app_domain").last().html(html);
				$(".app_domain").last().bind("change", function(){
					appDomainSelect = $(this);
					$.ajax({
						url : "<%url('uc/get_domains')%>?parent_id=" + $(this).val(),
						type: "get",
						async : isasync,
						success:function(data){
							if(data.success){
								var html = template("domainTemp", {list: data.data});
								appDomainSelect.parent().next().removeClass("hide");
								appDomainSelect.parent().next().find("select").html(html);
							}
						}

					})

				});
			}
		}
	})
});


function loadTechs(){
	$.ajax({
		url : "<%url('uc/get_techs')%>",
		type: "get",
		async : isasync,
		success:function(data){
			if(data.success && data.data.length){
				var html = template("domainTemp", {list: data.data});
				$(".save_tech").last().html(html);
			}
		}
	})
}


$("#addTech").bind("click", function(){
	var html = template("saveTechTemp", {});
	$(html).insertAfter($(".saveTechGroup").last());
	loadTechs();
	$(".deleteTech").unbind("click");
	$(".deleteTech").bind("click", function(){
		$(this).parents(".saveTechGroup").remove();
	})
});


$('#bank_copy').AjaxFileUpload({
	path : "<% url('uc/user/upload')%>",
	onComplete : function(filename){
			$("#enwName").html(filename.filename);
			$("#fileName").val(filename.filename);
		}
	});

$("input[type=radio]").bind("change", function(){
	if($(this).attr("value") == 1){
		hasCreditInfo = true;
		$(".bank_credit").removeClass("hide");
	}else{
		hasCreditInfo = false;
		$('input[name=bank_credit_level]').val('');
		$('input[name=bank_credit_copy]').val('');
		$('input[name=bank_card_code]').val('');
		$(".bank_credit").addClass("hide");
	}
});

$("#sub").click(function(){
	var staff_num = $("input[name='staff_num']").val().trim();
	var tech_num = $("input[name='tech_num']").val().trim();
	var senior_tech_num = $("input[name='senior_tech_num']").val().trim();
	var has_bank_credit_info = $("input[name='has_bank_credit_info']:checked").val().trim();
	var total_capital = $("input[name='total_capital']").val().trim();
	var net_capital = $("input[name='net_capital']").val().trim();
	var liabilities_rate = $("input[name='liabilities_rate']").val().trim();
	var note = $("textarea[name='note']").val().trim();


	var domains = [];
	$.each($(".app_domain"), function(index, item){
		if($(item).parent().next().hasClass("hide")){
			domains.push($(item).val());
		}else{
			domains.push($(item).parent().next().find("select").val());
		}
	});

	var bool = false;
	$.each(domains, function(index, item){
		if(item == ""){
			warning("", "请选择应用领域！");
			bool = true;
			return true;
		}
	});

	if(bool){
		return;
	}

	var techs = [];
	$.each($(".save_tech"), function(index, item){
		techs.push($(item).val());
	});

	$.each(techs, function(index, item){
		if(item == ""){
			warning("", "请选择节能技术！");
			bool = true;
			return true;
		}
	});

	if(bool){
		return;
	}

	var params = {
		staff_num : staff_num,
		tech_num : tech_num,
		senior_tech_num : senior_tech_num,
		has_bank_credit_info : has_bank_credit_info,
		total_capital : total_capital,
		net_capital : net_capital,
		liabilities_rate : liabilities_rate,
		note : note,
		domains: domains,
		techs: techs
	};

	if(hasCreditInfo){
		var bank_credit_level = $("input[name='bank_credit_level']").val().trim();
		var bank_credit_copy = $("input[name='bank_credit_copy']").val().trim();
		var bank_card_code = $("input[name='bank_card_code']").val().trim();
		if (bank_credit_level == '' || bank_credit_copy == '' || bank_card_code == '')
		{
			warning('','必填信息不能为空');
			return false;
		}
		params["bank_credit_level"] = bank_credit_level;
		params["bank_credit_copy"] = bank_credit_copy;
		params["bank_card_code"] = bank_card_code;
	}

	if (has_bank_credit_info == '' || total_capital == '' || net_capital == '' || liabilities_rate == '' )
	{
		warning('','必填信息不能为空');
		return false;
	}
	$.post("edit_industryinfo/update", params,function (data){
		if(data.success){
			success("", "提交成功");
			setTimeout(function(){
				window.location.href="industryinfo";
			}, 2000)
		}else{
			warning("", data.msg);
		}
	})
})

</script>
</html>